class Reward < ActiveRecord::Base
  belongs_to :raid
  belongs_to :mob
  has_many :characters_reward, :dependent => :destroy
  has_many :characters, :through => :characters_reward

  @@total_number_of_shares = nil

  def after_save
    Reward.update_total_number_of_shares
  end

  def self.total_number_of_shares
    if @@total_number_of_shares == nil
      update_total_number_of_shares
    end
    @@total_number_of_shares
  end

  def self.update_total_number_of_shares
    sum = Character.sum(:number_of_shares, :include => :rewards, :conditions => [ "active = ?", true ]).to_f
    @@total_number_of_shares = sum
  end

end
